package com.knowswift.issues.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.knowswift.common.bean.issue.po.Customer;
import com.knowswift.common.bean.issue.po.SysUser;
import com.knowswift.common.bean.issue.vo.CustomerVO;
import com.knowswift.common.bean.issue.vo.IssueVO;
import com.knowswift.common.bean.project.po.Project;
import com.knowswift.common.common.BaseResponse;
import com.knowswift.common.common.Page;
import com.knowswift.log.aop.AopLog;
import com.knowswift.common.bean.issue.vo.ModuleDto;
import com.knowswift.security.util.AllowVisitor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @author CTL
 * @create 2025-07-02 16:27
 */
@RestController
@RequestMapping("/customer")
public class AdminCustomerController extends AbstractController {

    @AopLog(saveLog = false, name = "查询客户分页", platform = "管理后台")
    @PostMapping("/page")
    @AllowVisitor(checkToken = false)
    public BaseResponse queryPage(@RequestBody CustomerVO param) {
        Page<IssueVO> page = new Page<>(param.getPageNo(), param.getPageSize());
        page = customerService.getBaseMapper().getCustomers(param.getCustomerCode(), param.getCustomerName(),
                param.getStatus(), page);
        return BaseResponse.success(page);
    }

    //@AopLog(saveLog = false, name = "查询客户详情", platform = "管理后台")
    @GetMapping("/info/{customerId}")
    @AllowVisitor(checkToken = false)
    public R info(@PathVariable("customerId") String customerId) {
        Customer customer = customerService.getById(customerId);
        if(customer==null){
            return R.failed("未查询到id为"+customerId+"的客户");
        }
        //获取客户账号
        List<SysUser> userList = sysUserService.getBaseMapper().getUserByCustomerId(customerId);
        customer.setUserList(userList);
        List<ModuleDto> moduleDtoList = authorizeService.getBaseMapper().getModuleByCustomerId(customerId);
        //获取客户授权项目
        for (ModuleDto module : moduleDtoList) {
            List<Project> projectList = authorizeService.getBaseMapper().getCustomerModuleProject(customerId, module.getModuleId());
            module.setProjects(projectList);
        }
        customer.setModuleList(moduleDtoList);
        return R.ok(customer);
    }



    @AopLog(saveLog = false, name = "新增客户", platform = "管理后台")
    @PostMapping("/save")
    @AllowVisitor(checkToken = false)
    public R save(@Valid @RequestBody Customer customer) {
        customerService.saveCustomer(customer);
        return R.ok("新增成功");
    }

    @AopLog(saveLog = false, name = "修改客户", platform = "管理后台")
    @PostMapping("/update")
    @AllowVisitor(checkToken = false)
    public R update(@Valid @RequestBody Customer customer) {

        customerService.updateById(customer);
        return R.ok("修改成功");
    }

    @AopLog(saveLog = false, name = "删除客户", platform = "管理后台")
    @DeleteMapping("/delete")
    @AllowVisitor(checkToken = false)
    public R delete(@RequestBody String[] customerIds) {
        for (String id : customerIds){
            List<SysUser> userList = sysUserService.getBaseMapper().getUserByCustomerId(id);
            if(userList!=null){
                return R.failed("id为"+id+"的客户下有账户无法删除");
            }
        }
        customerService.removeByIds(Arrays.asList(customerIds));
        return R.ok("删除成功");
    }
    @AopLog(saveLog = false, name = "授权客户项目", platform = "管理后台")
    @PostMapping("/Auth")
    @AllowVisitor(checkToken = false)
    public R ProjectAuthorize(@RequestParam String customerId, @RequestBody List<ModuleDto> moduleList){
        Customer customer = customerService.getById(customerId);
        if(customer==null){
            return R.failed("未查询到id为"+customerId+"的客户");
        }
        customerService.ProjectAuthorize(customerId,moduleList);
        return R.ok("授权成功");
    }

}
